/**
 * <p>Title: Snake</p>
 * <p>Copyright: (C) 2011 Gavin's Snake project. Licensed under the Apache License, Version 2.0 (the "License")</p>
 * @author Gavin
 */

package com.deaboway.snake;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.deaboway.snake.util.BaseRMS;
import com.deaboway.snake.util.Bundle;
import com.deaboway.snake.util.Log;

/**
 * Snake: a simple game that everyone can enjoy.
 * 
 * 贪吃蛇： 经典游戏，在一个花园中找苹果吃，吃了苹果会变长，速度变快。碰到自己和墙就挂掉
 * 
 */
public class Snake extends MIDlet {

	public static Display display;
	public static SnakeView mSnakeView;
	public static MIDlet SNAKE;
	
	public Snake() {
		Bundle.INIT();
		display=Display.getDisplay(this);
		SNAKE = this;

		mSnakeView = new SnakeView();
		mSnakeView.setTextView("");
		mSnakeView.setMode(SnakeView.READY);
		display.setCurrent(mSnakeView);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		mSnakeView.saveState();
	}

	protected void pauseApp() {
		mSnakeView.setMode(SnakeView.PAUSE);
	}

	protected void startApp() throws MIDletStateChangeException {
		// 检查存贮状态以确定是重新开始还是恢复状态
		Log.debug("startApp(), BaseRMS.FIRST="+BaseRMS.FIRST);
		if (BaseRMS.FIRST) {
			// 存储状态为空，说明刚启动可以切换到准备状态
			mSnakeView.setMode(SnakeView.READY);
		} else {
			// 已经保存过，那么就去恢复原有状态
			// 恢复状态
			if (!mSnakeView.restoreState()) {
				// 恢复状态不成功，设置状态为暂停
				mSnakeView.setMode(SnakeView.PAUSE);
			}
		}
	}

}
